/* 
 * File:   City.cpp
 * Author: Faisal
 *
 * Created on April 27, 2012, 8:39 PM
 *
 * Description:
 *		this is auto generated class and the implementation will be replaced whenever persistO code generator is exectued.
 *
 */

#include "../Include/City.hpp"

using Supetronix::PersistO::BO::City;

/*
*	All table specific "insert, update, delete and select" queries will be generated and set to the internal variable.
*/
const string City::_selectQuery = "SELECT * FROM City";


/*
*	class methods
*/
//	Internal method that will be call by the query modifier. It will have the logic to call the propery data context provider.
City* City::execute(QueryType qryType, const string& query, const QueryModifierBase* queryModifier)
{
    auto dc = MySqlDC::getInstance();

	//call the execute method of underlying data context and check if modified query is passed. If no query is passed then use the 
	//internal generated query. Once query is executed then store the data in the internal resultset (declare in BusinessBaseData).
    _resultSet = dc->executeQuery(ConnectionSourceName::Test, qryType, query.empty() ? _selectQuery : query, queryModifier);

    dc = nullptr;
    return this;
}

//this is one of the get method generated by the code generator for the specific table columns.
unique_ptr<City> City::getByCityID(int id)
{
	//this code need to be moved to "some application start-up" code. The application start-up code will read some config file and load connection data.
    ConnectionDefinition::Add(ConnectionSourceName::Test, ConnectionSourceType::MySQL, "tcp://127.0.0.1:3306", "root", "root123", "test");
    
	//call the "Select" method to instantiate the QueryModifier class and handle the filters. Once filters are added then call the execute method to invoke
	//the execute method of business class.
    unique_ptr<City> data( City::Select()->addFilter(FilterType::Equal, "CityID", id)->execute() );

	//This line will be moved to the "Next" method of business class
    data->loadData( data->_resultSet );

	//it will return the empty business object and user code will need to call next to get the first valid data loaded.
    return data;
}

//this method will set the business object's internal data elements with the data from underlying data source
bool City::loadData(sql::ResultSet* result)
{
    if ( result->next())
    {
            _cityID = result->getInt("cityid");
            _cityName = result->getString("cityname");
    }

    return true;
}

